package com.wood.life.rabbitma;


import com.night.common.entity.life.*;
import com.rabbitmq.client.Channel;
import com.wood.life.mapper.AnswerMapper;
import com.wood.life.mapper.ImageMapper;
import com.wood.life.mapper.QuestionMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * author: dz
 * 2022-09-22-11:13
 */

@Component
@Transactional
public class AnswerConsumer {

    @Autowired
    AnswerMapper answerMapper;

    @Autowired
    ImageMapper imageMapper;
    //处理消息可以添加三个参数
    //String str  消息正文
    //Message message 消息对象
    //Channel channel 信道

    @RabbitListener(queues = "queueanswer")
    public void receiveDelayplugin(AnswerVo answerVo, Message message, Channel channel) throws IOException {
        System.out.println("answerVo:" + answerVo);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        System.out.println("查询修改订单状态：" + new Date());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("--------");
        answerMapper.insert(answerVo.getAnswer());
        Answer answer = answerVo.getAnswer();

        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(date.format(new Date()));// new Date()为获取当前系统时间
        answer.setAnswerTime(date.format(new Date()));

        answerMapper.insert(answer);
        System.out.println("添加问题");
        if (answerVo.getImageUrl() == null) {
            System.out.println("没有图片，成功");
            return;
        }
        Image image = new Image();
        image.setImageUrl(answerVo.getImageUrl());
        image.setType("answer");
        image.setTypeId(answer.getId());
        imageMapper.insert(image);
        System.out.println("成功");

    }
}
